define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'trackdetail/index' + location.search,
                    add_url: 'trackdetail/add',
                    edit_url: 'trackdetail/edit',
                    del_url: 'trackdetail/del',
                    multi_url: 'trackdetail/multi',
                    import_url: 'trackdetail/import',
                    table: 'track_detail',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
				searchFormVisible: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),operate:false,visible: false},
                        {field: 'track.id', title: __('Track_id'),visible: false},
                        //{field: 'track_id', title: __('Track_id')},
                        {field: 'department.name', title: __('Department'), operate: 'LIKE'},
                        //{field: 'department_id', title: __('Department_id')},
                        //{field: 'group_id', title: __('Group_id')},
                        {field: 'preparer', title: __('Preparer'), operate: 'LIKE'},
                        {field: 'year', title: __('Year')},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        //{field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        //{field: 'group.name', title: __('Group.name'), operate: 'LIKE'},
                        //{field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            formatter: function (value, row, index) {
                                var timestamp = Date.parse(new Date());
                                var times = timestamp/ 1000;
                                //console.log(Config.rules);
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                if (row.endtime < times) {
                                    $(table).data("operate-edit", null);
                                }
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        }

                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
